package com.zx.platform.utils;

import java.util.UUID;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

public class AlipayUtil {
	public final static String APP_ID="2017102409499740";
	public final static String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDVIZAtuQ+BoiutkG3JGu+NNsNh6iD+f/9x8uoVgdW8EgGSvJBZn0nMo4Av1rGTyU78Xz6rmcspRWIubojenwUIE1Ss4v2/oNDTNtF+B///JCVpIoxlovuiHwLUk7zAHfJlktKvzwD8xj19EvnvtElHVkJV4eO4lyHv8/re9lwPRQRrynv//B1HLsgfaxXhpfWaeajzRSDm5BzY/L0JAquExU0GsIpH+VX1yGdFq1+MhobYUCHtcl6DBTvRNYIM3L1oRwEZ+yHhA9Thx1Eo+emS2RwsCiocEBYAk+aWXrCAisvsF8YlqS8p2xQAjBMrp/N9HHi8l5EbS4Pz27o+R6mpAgMBAAECggEAODwe2rGVkdLES4tsLD/bLRth+hfYlM4ZDGvXr80OJA1sNNdkDwGka0ZIsFapjCD1oklPM9/kZwNToISxwGuc3ECdevB5m3JHJzOzmtlphlx3MoJXIQob8u7TxFsm1loQdxSCWrj3vbpgxvuWztR28UelQRvVBgN6hOGPM/001SIZcHLla8gv5U0F1RbXzAcZfPnFpGf8PAEYa/yOnIgKbnUtBsZtlC5dKWv28ITkyRRH0V370aH/B3Dj0lm8fQQio0e8tT7ksotgr6JlDwDx7kIqzbaLOcrkYPeebFKb/xgM4u/Wfbx37pqBkgzotSO14+3XFos0hXkytgCyx3xqAQKBgQDxN9x91i4pdWfcs7YwgWEJs8/QdlgCPpwjgkcV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V/hGT7hUIofbv2ihE/CnbnFzXJznEaEeha+v/imqZUZlT34HsrQzf6PsEodDBvhTwd+IOEk2P8ucg3O/YqQnQmfIPE+PrwXjihtjrDwH/DRL1x2aZMZV0hFt5gOuAbEUrX5PmqBbSHKmo5ORviMV+KFiSB/58Y5egtXGVjjeKLDDkL+SwIDAQAB";
	
	public static String payOrder(String orderNo,String totalPay,String productName,String merchantId) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
		AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
		request.setNotifyUrl("http://116.62.116.58:8080/scp/income/alipay/nofityPay");
		request.setBizContent("{" +
		"    \"out_trade_no\":\""+orderNo+"\"," +
		"    \"total_amount\":\""+totalPay+"\"," +
		"    \"subject\":\""+productName+"\"," +
		"    \"store_id\":\""+merchantId+"\"," +
		"    \"timeout_express\":\"90m\"}");//设置业务参数
		AlipayTradePrecreateResponse response = alipayClient.execute(request);
		return response.getQrCode();
	}
	
	public static void queryOrder(String orderNo) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
		AlipayTradeQueryRequest r = new  AlipayTradeQueryRequest();
		r.setBizContent("{ \"out_trade_no\":\""+orderNo+"\""+"}");
		AlipayTradeQueryResponse response = alipayClient.execute(r);
		System.out.println(response.getBody());
	}
	
	private static final String refund_reason="红包退款";
	
	public static void refundOrder(String orderNo,String refundAmount) throws AlipayApiException {
		String out_request_no=UUID.randomUUID().toString();
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
		AlipayTradeRefundRequest r = new  AlipayTradeRefundRequest();
		r.setBizContent("{" +
				"    \"out_trade_no\":\""+orderNo+"\"," +
				"    \"refund_amount\":\""+refundAmount+"\"," +
				"    \"refund_reason\":\""+refund_reason+"\"," +
				"    \"out_request_no\":\""+out_request_no+"\"}");
		AlipayTradeRefundResponse response = alipayClient.execute(r);
		System.out.println(response.getBody());
	}
	
	public static void main(String[] args) throws AlipayApiException {
		//payOrder("20111171711111","0.01","ttt","m1");
		//queryOrder("20111171711111");
		AlipayUtil.refundOrder("P2017102711025328069205","10.00");
	}
}
